package com.example.es.controller;

import com.example.es.pojo.Products;
import com.example.es.service.ProductsService;
import com.google.gson.Gson;
import org.apache.kafka.common.protocol.types.Field;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/es")
public class ProductsController {
    @Autowired
    private ProductsService productsService;
    @Autowired
    private RestHighLevelClient restHighLevelClient;
    private Gson gson = new Gson();
    @RequestMapping("/query/{name}")
    public Map<String,Object> queryByName(@PathVariable String name) throws IOException {
        HashMap<String,Object> map = new HashMap<>();
        //创建搜索请求对象
        SearchRequest request = new SearchRequest();
        //查询构建工具
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        //添加查询条件，执行查询类型
        sourceBuilder.query(QueryBuilders.matchQuery("name",name));
        //执行排序 价格降序排序
        sourceBuilder.sort("price", SortOrder.ASC);
        //分页信息
        int pageNumber = 1;
        int pageSize = 5;
        int from = (pageNumber-1)*pageSize;
        //设置分页
        sourceBuilder.from(from);
        sourceBuilder.size(5);
        HighlightBuilder highlightBuilder = new HighlightBuilder().field("name").preTags("<en>").postTags("</en>");
        sourceBuilder.highlighter(highlightBuilder);
        SearchHits hits = baseQuery(sourceBuilder);
        System.out.println(hits.toString());
        //获得文件数组
        SearchHit[] hitsHits = hits.getHits();
        ArrayList<Products> list = new ArrayList<>();
        for(SearchHit searchHit: hitsHits){
            Text[] names = searchHit.getHighlightFields().get("name").getFragments();
            String s = names[0].toString();
            String json = searchHit.getSourceAsString();
            //将json反序列化为Product格式
            Products product = gson.fromJson(json, Products.class);
            product.setName(s);
            list.add(product);
            System.out.println(product);
        }
        map.put("start",from);
        map.put("size",hitsHits.length);
        map.put("list",list);
        return map;
    }
    public SearchHits baseQuery(SearchSourceBuilder sourceBuilder) throws IOException {
        //创建搜索请求对象
        SearchRequest request = new SearchRequest();
        //查询构建工具
        request.source(sourceBuilder);
        //执行查询
        SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
        //获得查询结果
        SearchHits hits = response.getHits();
        return hits;
    }
}
